﻿using CoAP.Net;
using CoAP.Server;
using Microsoft.Extensions.Configuration;
using System.Configuration;

namespace NbMeterServices
{
    public class NbService
    {
        private CoapServer server = null;
        private IConfiguration _config;
        public NbService(IConfiguration config)
        {
            _config = config;
            var url = config["NB:CoapService:Urls"].ToString();
            var port = int.Parse(config["NB:CoapService:Port"].ToString());
            InitNbService(url, port);
        }

        public void InitNbService(string uri, int port)
        {
            server = new CoapServer();

            // 按照URI参数分隔
            //string[] uriParts = uri.Split('/');

            //// 创建根资源
            //var rootResource = new CoapResource(uriParts[0]);

            //// 逐个处理URI参数中的部分，并创建对应的资源
            //for (int i = 1; i < uriParts.Length; i++)
            //{
            //    var resource = new CoapResource(uriParts[i]);
            //    rootResource.Add(resource);
            //    rootResource = resource; // 将当前资源设为父资源，以便处理下一个URI部分
            //}

            //var service = new CoapResource("sensors").Add(new CoapResource("temp"));
            //server.AddEndPoint(new CoAPEndPoint(5006));
            //server.Add(service);



            server.AddEndPoint(new CoAPEndPoint(port));
            server.Add(new CoapResource("sensors", _config).Add(new CoapResource("temp", _config)));
            ////server.Add(rootResource);
            //server.AddEndPoint(new CoAPEndPoint(10000));
            server.Start();
        }
    }
}
